/* >fe-web> node ./config/card/card-editable-params.build.js */
// const express = require("express");
const path = require("path");
const fs = require("fs");

const _filePath = path.resolve(__dirname, "../../", "./src/views/card/js/data/card-datas-editable-params.js");
const _rootDistPath = path.resolve(__dirname, "../../", "./src/views/card/js/data/cardDatasEditableParams/cardEditableParamsLists");
fs.readFile(_filePath, { encoding: "utf-8" }, function(err, data) {
  if (err) throw err;
  // console.log(data);

  // return;

  // export const CardEditableParamsLists \= \{((.|\n)*)?
  const _reg1 = /export const CardEditableParamsLists \= \{(.|\n)*?\}\;/;
  const _match = data.match(_reg1)
  // console.log(_match[0]);

  const _matchCardEditableParamsLists = _match[0];
  const _reg2 = /(CARD_|CARD_JY)(\d)+\:(\s)?\{/g;
  const _matchCard = _matchCardEditableParamsLists.match(_reg2);
  // console.log(_matchCardEditableParamsLists);
  console.log(_matchCard.length);

  _matchCard.forEach(str => {
    console.log(str);
  })

  const _placeholder = "__placeholder__";
  const _newCardEditableParamsLists = _matchCardEditableParamsLists.replace(_reg2, _placeholder);
  // console.log(_newCardEditableParamsLists);
  let _ArrayCardEditableParamsLists = _newCardEditableParamsLists.split(_placeholder);
  console.log(_ArrayCardEditableParamsLists.length);

  let _indexImportStr = "";
  let _indexExportStr = "export {";

  _ArrayCardEditableParamsLists.shift(0);
  let len = _ArrayCardEditableParamsLists.length;
  _ArrayCardEditableParamsLists = _ArrayCardEditableParamsLists.map((card, index) => {
    // console.log(card);
    _newCard = _matchCard[index] + card;
    _reg3 = /\n\}\;$/;
    _newCard = _newCard.replace(_reg3, "");
    _reg4 = /\,(\s)*$/;
    _newCard = _newCard.replace(_reg4, ";");
    // _newCard = _newCard.replace(/\s/g, "");
    _reg6 = /^(CARD_|CARD_JY)(\d+)(\:)(\s?\{)/;
    _newCard = _newCard.replace(_reg6, "$1$2 = $4");
    const _import = `import { CardEditableParamsDefault, CardDataEditableParamsCommonDefault } from "@/views/card/js/data/card-datas-default.js";
import TooltipsData from "@/views/card/js/data/tooltipsData/tooltips-data.js";\n`
    _newCard = _import + "const " + _newCard;

    const _reg7 = /(CARD_|CARD_JY)(\d)+/;
    const _fileName = _matchCard[index].match(_reg7)[0];
    _newCard = _newCard + `\nexport default ${_fileName};`

    console.log(_newCard);
    return _newCard;
  }).forEach((card, index) => {
    const _reg5 = /(CARD_|CARD_JY)(\d)+/;
    let _fileName = _matchCard[index].match(_reg5)[0];
    let _filePath = path.resolve(_rootDistPath, _fileName + ".js");

    _indexImportStr += `import ${_fileName} from "./${_fileName}.js";\n`
    _indexExportStr += (` ${_fileName}` + ( index==len-1?" }":"," ))

    console.log(_fileName);
    fs.writeFile(_filePath, card, { encoding: "utf8" }, (err) => {
      if(err) console.log(_fileName, " : ", err);
      else console.log(_fileName, " : success!");
    })
  })

  fs.writeFile(path.resolve(_rootDistPath, "./index.js"), _indexImportStr + _indexExportStr, { encoding: "utf8" }, (err) => {
    if(err) console.log("index : ", err);
    else console.log("index : success!");
  })
})
